package com.zenithmind.classroom.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.classroom.pojo.dto.ChapterCreateDTO;
import com.zenithmind.classroom.pojo.dto.ChapterUpdateDTO;
import com.zenithmind.classroom.pojo.dto.ChapterQueryDTO;
import com.zenithmind.classroom.pojo.vo.ChapterVO;
import com.zenithmind.classroom.service.CourseChapterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程章节控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "课程章节管理", description = "课程章节管理相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/chapters")
@RequiredArgsConstructor
public class CourseChapterController {

    private final CourseChapterService courseChapterService;

    @Operation(summary = "创建章节", description = "创建新的课程章节")
    @PostMapping
    public Result<ChapterVO> createChapter(@Valid @RequestBody ChapterCreateDTO createDTO) {
        ChapterVO chapter = courseChapterService.createChapter(createDTO);
        return Result.success(chapter);
    }

    @Operation(summary = "更新章节", description = "更新章节信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateChapter(
            @Parameter(description = "章节ID") @PathVariable String id,
            @Valid @RequestBody ChapterUpdateDTO updateDTO) {
        Boolean result = courseChapterService.updateChapter(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除章节", description = "删除指定章节")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteChapter(@Parameter(description = "章节ID") @PathVariable String id) {
        Boolean result = courseChapterService.deleteChapter(id);
        return Result.success(result);
    }

    @Operation(summary = "发布章节", description = "发布指定章节")
    @PostMapping("/{id}/publish")
    public Result<Boolean> publishChapter(@Parameter(description = "章节ID") @PathVariable String id) {
        Boolean result = courseChapterService.publishChapter(id);
        return Result.success(result);
    }

    @Operation(summary = "下架章节", description = "下架指定章节")
    @PostMapping("/{id}/unpublish")
    public Result<Boolean> unpublishChapter(@Parameter(description = "章节ID") @PathVariable String id) {
        // TODO: 实现章节下架功能
        return Result.success(true);
    }

    @Operation(summary = "分页查询章节", description = "分页查询章节列表")
    @GetMapping("/page")
    public Result<PageResult<ChapterVO>> getChapterPage(@Valid ChapterQueryDTO queryDTO) {
        PageResult<ChapterVO> result = courseChapterService.getChapterPage(queryDTO);
        return Result.success(result);
    }

    @Operation(summary = "获取课程章节列表", description = "获取指定课程的章节列表")
    @GetMapping("/course/{courseId}")
    public Result<List<ChapterVO>> getCourseChapters(
            @Parameter(description = "课程ID") @PathVariable String courseId,
            @Parameter(description = "是否包含未发布") @RequestParam(defaultValue = "false") Boolean includeUnpublished) {
        List<ChapterVO> chapters = courseChapterService.getCourseChapters(courseId, includeUnpublished);
        return Result.success(chapters);
    }

    @Operation(summary = "获取章节详情", description = "获取章节详细信息")
    @GetMapping("/{id}")
    public Result<ChapterVO> getChapterDetail(@Parameter(description = "章节ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        ChapterVO chapter = courseChapterService.getChapterDetail(id, userId);
        return Result.success(chapter);
    }

    @Operation(summary = "调整章节顺序", description = "调整章节排序")
    @PostMapping("/reorder")
    public Result<Boolean> reorderChapters(
            @Parameter(description = "课程ID") @RequestParam String courseId,
            @Parameter(description = "章节ID列表") @RequestBody List<String> chapterIds) {
        Boolean result = courseChapterService.reorderChapters(courseId, chapterIds);
        return Result.success(result);
    }

    @Operation(summary = "复制章节", description = "复制现有章节")
    @PostMapping("/{id}/copy")
    public Result<ChapterVO> copyChapter(
            @Parameter(description = "章节ID") @PathVariable String id,
            @Parameter(description = "目标课程ID") @RequestParam String targetCourseId,
            @Parameter(description = "新章节标题") @RequestParam String title) {
        ChapterVO chapter = courseChapterService.copyChapter(id, targetCourseId, title);
        return Result.success(chapter);
    }

    @Operation(summary = "移动章节", description = "移动章节到其他课程")
    @PostMapping("/{id}/move")
    public Result<Boolean> moveChapter(
            @Parameter(description = "章节ID") @PathVariable String id,
            @Parameter(description = "目标课程ID") @RequestParam String targetCourseId,
            @Parameter(description = "新排序值") @RequestParam Integer sortOrder) {
        Boolean result = courseChapterService.moveChapter(id, targetCourseId, sortOrder);
        return Result.success(result);
    }

    @Operation(summary = "获取章节统计", description = "获取章节统计信息")
    @GetMapping("/{id}/statistics")
    public Result<Object> getChapterStatistics(@Parameter(description = "章节ID") @PathVariable String id) {
        Object statistics = courseChapterService.getChapterStatistics(id);
        return Result.success(statistics);
    }

    @Operation(summary = "更新章节统计", description = "更新章节统计信息")
    @PostMapping("/{id}/statistics/update")
    public Result<Boolean> updateChapterStatistics(@Parameter(description = "章节ID") @PathVariable String id) {
        Boolean result = courseChapterService.updateChapterStatistics(id);
        return Result.success(result);
    }

    @Operation(summary = "批量导入章节", description = "批量导入章节数据")
    @PostMapping("/batch/import")
    public Result<Integer> batchImportChapters(@Valid @RequestBody List<ChapterCreateDTO> chapters) {
        Integer count = courseChapterService.batchImportChapters(chapters);
        return Result.success(count);
    }

    @Operation(summary = "导出章节", description = "导出指定章节数据")
    @PostMapping("/batch/export")
    public Result<List<ChapterVO>> exportChapters(@RequestBody List<String> chapterIds) {
        List<ChapterVO> chapters = courseChapterService.exportChapters(chapterIds);
        return Result.success(chapters);
    }
}
